package cn.iocoder.yudao.module.jifenyuan.job;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.jifenyuan.dal.dataobject.wallet.WalletDO;
import cn.iocoder.yudao.module.jifenyuan.dal.mysql.wallet.WalletMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@TenantJob
@Slf4j
public class VipExpireJob implements JobHandler {
    @Resource
    private WalletMapper walletMapper;

    /**
     *          每天凌晨执行定时任务          0 0 0 * * ?
     * @param param 参数
     * @return
     * @throws Exception
     */
    @Override
    public String execute(String param) throws Exception {
        List<WalletDO> walletDOS = walletMapper.selectVipExpireList();
        List<WalletDO> walletUpdates = new ArrayList<>();
        for (WalletDO walletDO : walletDOS) {
            log.info("修改wallet id {} vip为false",walletDO.getId());
            WalletDO update = new WalletDO();
            update.setId(walletDO.getId());
            update.setVip(false);
            update.setVipExpireTime(null);
            walletUpdates.add(update);
        }
        if(walletUpdates.size()>0){
            walletMapper.updateBatch(walletUpdates);
        }

        return "修改数量为"+walletUpdates.size();
    }
}
